--
-- @Author:      name
-- @DateTime:    2020-04-30 23:05:48
-- @Description: 生成视讯订单号id

local _ENV,_START = hf_setenv('oder_id',true)

local skynet = require "skynet"
local server_config = dofile(skynet.getenv("server_config"))
local util = require "util"

local table_remove = table.remove
local table_insert = table.insert
local math_random = math.random




function init()
    server_id = 1 --服务节点id
    count = 0

    hf_set_sentinel_end(_ENV)
end

---------------------------------------------------------
-- CMD
---------------------------------------------------------
local handler = {}


function handler.start()
	server_id = tonumber(skynet.getenv("server_id")) or 1
	print("__start___order_id_service_", server_id)
	if server_id < 0 or server_id > 7 then 
		errlog("________server_id___", server_id)
		server_id = 7
	end	
	skynet.retpack(true)
end


function handler.get_order_id(uid)
	local time = os.time()
	count = count + 1
	if count > (1 << 20) - 1 then 
		count = 0
	end

	local rand_num = math_random(1, 200)
	local num1 = server_id << 60 --代表60-63存放server_id
	local num2 = count << 40 -- 40-60存放count
	local num3 = rand_num << 32  --剩下8位放随机值
	local order_id = num1 | num2 | num3 | time
	order_id = ""..order_id
	print("___get_order_id___",uid , order_id)
    skynet.retpack(true, order_id)
end




---------------------------CMD--------------------------------
function dispatch_cmd(session,source,action,...)
    local f = assert(handler[action],'corder_id_service---->unknown cmd ' .. action)
    f(...)
end

function start()
    skynet.start(function()
        skynet.dispatch("lua",function(session,addr,action,...)
            dbglog('============got params ...... ',action,...)
            dispatch_cmd(session,addr,action,...)
        end)
    end)
end

_START(function()
    print('now this service is starting')
    init()
    start()
end)
